package com.chinaunicom.system.controller;

import java.util.HashMap; 
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;



import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaunicom.system.dto.UserDto;
import com.chinaunicom.system.model.Role;
import com.chinaunicom.system.model.User;
import com.chinaunicom.system.service.IRoleService;
import com.chinaunicom.system.service.IUserService;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/sys/user")
public class UserController {

	@Resource
	private IUserService userService;

	@Resource
	private IRoleService roleService;

	@ResponseBody
	@RequestMapping("/queryData")
	public Map<String, Object> queryData(UserDto dto, String message,HttpServletRequest request)
			throws Exception {
		
		request.getParameter("pageNumber");
		request.getParameter("pageSize");
		
		dto.setMessage(message);
		Map<String, Object> map = new HashMap<>();
		List<UserDto> user = userService.query(dto);
		map.put("total", user.size());
		map.put("rows", user);
		return map;
	}

	@RequestMapping("/query")
	public String query() throws Exception {
		return "/WEB-INF/system/user_list.jsp";
	}

	/**
	 * 查询进入添加用户数据界面之前需要的准备数据 所有的角色
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insert")
	public String insert(Model model) throws Exception {
		// 查询所有角色
		List<Role> role = roleService.query();
		model.addAttribute("role", role);
		return "/WEB-INF/system/user_add.jsp";

	}

	/**
	 * 添加用户信息
	 * 
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(UserDto dto) throws Exception {
		// 保存相关的数据
		userService.save(dto);
		return "redirect:/sys/user/query";
	}

	/**
	 * 查询进入更新界面之前需要的数据 1.根据id获取原来的数据 2.根据id获取用户对应的所有的角色编号 3.获取所有的角色信息
	 * 
	 * @param id
	 * @param model
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateData")
	public String updateData(Integer id, Model model) throws Exception {
		// 1.根据id获取用户信息
		UserDto dto = userService.queryById(id);
		model.addAttribute("dto", dto);
		// 2.取出用户对应的角色信息
		List<Role> userRoles = dto.getRoleList();
		model.addAttribute("userRoles", userRoles);
		// 3.查询出所有角色
		List<Role> roles = roleService.query();
		model.addAttribute("roles", roles);
		return "/WEB-INF/system/user_update.jsp";
	}

	@RequestMapping("/update")
	public String update(UserDto dto) throws Exception {
		userService.update(dto);
		return "redirect:/sys/user/query";

	}

	@RequestMapping("/delete")
	public String delete(String ids) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		int[] idsData = mapper.readValue(ids, int[].class);
		for (int id : idsData) {
			userService.delete(id);
		}
		return "redirect:/sys/user/query";
	}

	@RequestMapping("/own")
	public String own(HttpServletRequest request, Model model) throws Exception {
		HttpSession session = request.getSession();
		UserDto own = userService.queryById((int) session.getAttribute("id"));
		model.addAttribute("own", own);
		return "/WEB-INF/system/user_ownupdate.jsp";
	}

	@RequestMapping("/ownupdate")
	public String ownupdate(UserDto dto) throws Exception {
		User user = dto.getUser();
		userService.updateByPrimaryKeySelective(user);
		return "redirect:/sys/user/query";
	}

	@RequestMapping("/pwd")
	public String pwdchange() throws Exception {
		return "/WEB-INF/system/user_pwdchange.jsp";
	}

	@RequestMapping("/pwdChange")
	public String pwdchange(String pwd,String npwd,String epwd,HttpServletRequest request,Model model, UserDto dto) throws Exception{
		HttpSession session = request.getSession();
		UserDto own = userService.queryById((int)session.getAttribute("id"));
		model.addAttribute("own", own);
		System.out.println(own.getUser().getvPassword());
		
		if ((pwd != null && !("".equals(pwd))) && (npwd != null && !("".equals(npwd))) && (epwd != null && !("".equals(epwd)))) {
			if (!(pwd.equals(own.getUser().getvPassword()))) {
				model.addAttribute("pwd", "原密码不正确！");
				return "/WEB-INF/system/user_pwdchange.jsp";
			}else if (!(npwd.equals(epwd))) {
				model.addAttribute("mes", "两次密码输入不一致！");
				return "/WEB-INF/system/user_pwdchange.jsp";
			}
			
		}
			User user = dto.getUser();
			user.setvPassword(npwd);
			user.setnUserid((int)session.getAttribute("id"));
			userService.updateByPrimaryKeySelective(user);
			request.getSession().setAttribute("isLogin", "NO");
		
			
			return "redirect:/sys/user/query";
		
		
	}
}
